#include "CStr.h"

#include <windows.h>
#include <Notify.h>

#define VERSION L"ver 1.53"

#define g_szMessage L"Welcome to use \n \
jwz's Tornado brightness tools\n\n \
here is parameter list,\n \
-D0, \n \
-D1, \n \
-D2, \n \
-D3, (default option.)\n \
-D4, \n \
-H(Help)\n "

#define g_szHelp2 L"\
-Boot (use in startup's shortcut for boot loader half light!) \n \
-Silent (this option in shortcut, if jtb.exe \
in memory, will create a shortcut to \\windows\\startup directory). \n \
-[7:30\"/Windows/autoprof.exe\",\"-s Normal\";22:30\"/Windows/autoprof.exe\",\"-s Silent\"] \n \
-Profiles (this option with -Recovery, will restore destroyed Profiles) \n \
-(Bts)[c][d] (Blue tooth toggle) \n \
-Lock (Keyboard lock when screen off) \n \
-Always[120] (Always lock keyboard in given Time out.) \n\n \
Wolf&Lion Software Lab. Presents. \n \
Designed by jeewood. "

#define g_szHelp1 L"\n \
For more information \n \
Please use Option -Help\n \
Designed by jeewood. "

static TCHAR* g_szAppWndClass = TEXT("jTB ver 1.5");
static TCHAR* jTornadoBrightness_reboot_Shortcut = L"\\Windows\\Startup\\jTornadoBrightness_reboot_760211.lnk";

void SetLightOn();

#define CP_UNICODE                  65111       // 2 byte unicode
#define CP_UNICODE_PREFIX           65112       // 2 byte unicode with prefix
#define CP_UTF8_PREFIX              65115       // UTF-8 with prefix

class CmdX{
	enum{
		TOKEN_NULL,
		TOKEN_OPTION,
		TOKEN_BLOCK_BEGIN,
		TOKEN_BLOCK_END,
		TOKEN_NUMBER,
		TOKEN_PARAM,
		TOKEN_SEMICOLON,
		TOKEN_COLON,
		TOKEN_STRING,
		TOKEN_DOT,
		TOKEN_SILENT,
		TOKEN_BOOT,
		TOKEN_RECOVERY,
		TOKEN_HELP,
		TOKEN_LOCK,
		TOKEN_ALWAYS,
		TOKEN_BLUETOOL,
		TOKEN_BT_DISCOVERY,
		TOKEN_SPACE,
		TOKEN_FLIGHT,
		TOKEN_PROFILE,
		TOKEN_ON,
		TOKEN_OFF,
		TOKEN_D0,
		TOKEN_D1,
		TOKEN_D2,
		TOKEN_D3,
		TOKEN_D4,
#ifdef HEADSET
		TOKEN_AUTOHEADSET,
#endif
		TOKEN_TIMEFORMATERROR
	};

	typedef struct _TOKENS{
		TCHAR *string;
		int ID;
	}TOKENS;

	typedef struct _Token{
		int id;
		CStr val;
		int l;
	}Token;

	typedef struct _TIME{
		DWORD hour;
		DWORD minute;
		DWORD second;
		DWORD week;
	}TIME;

	typedef struct _EXE{
		DWORD w;
		time_t t;
		CStr e;
		CStr p;
		CStr path;
		_EXE * n;
	}EXE;

	static TOKENS TOKEN[];

	EXE *exe,*texe;

	int CmdLineLength;
	CStr CmdLine;
	
	CStr Week;
	CStr ExeFile;
	CStr JtbFile;
	CStr IniFile;
	CStr ExePath;

	WORD CurrWeek;
	int pos;
	TCHAR ch;
	TCHAR nch;
	TCHAR pch;
	Token pt;
	Token t;
	
	int NotifyRun;

	int Recovery;
	int Boot;
	int Lock;
	int Always;
	int BlueTool;
	int BlueToolState;
	int Flight;
	int Help;
	int DLevel;
	int Profile;
	int Headset;

	Run(CStr App,CStr Param);

	int GetToken();
	int GetWeek();
	SynErr(int TokenType);
	Expect(int TokenType);

	GetCh();
	AddCh();
	Get();
	GetTimer();

	UnixTimeToSystemTime(time_t t, LPSYSTEMTIME pst);
	time_t SystemTimeToUnixTime( const SYSTEMTIME &st );
	SYSTEMTIME GetNextExecute();
	WriteToFile(CStr FileName, CStr Content);

	int	ReadFile( LPCTSTR filename, CStr &content, int size=0, int cp = CP_ACP);
	bool FileOrDirExists( CStr &file, bool dir = false);
	bool isWeekOn(DWORD weeks,DWORD w);

	RemoveNotify(CStr App, CStr param );

	SetNotify(SYSTEMTIME st);
#ifdef HEADSET
	SetNotify(DWORD EventType);
#endif

	SetBlueTool(int Bt);

public:

	CmdX(CStr CommandLine);
	Parse();

	bool ExistBootShortCut();

	int GetLock();
	int GetBoot();
	int GetAlways();
	int GetDLevel();
	CStr GetModuleFile();
	
	RunAtTime();

};
